package 力扣._3_无重复字符的最长子串;

import java.util.HashSet;
import java.util.Set;

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        //滑动窗口
        Set<Character> set = new HashSet<>();
        int max = 0;
        for (int i = 0, j = 0; j < s.length(); j++) {
            char c = s.charAt(j);
            if (set.contains(c)) {
                while (set.contains(c) && i < j) {   //最多有1个重复
                    set.remove(s.charAt(i++));
                }
                set.add(c);
            } else {
                set.add(c);
                max = Math.max(max, set.size());
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int i = new Solution().lengthOfLongestSubstring("aab");
        System.out.println(i);
    }
}
